package mediatheque.view;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentListener;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import tools.ComboItem;
import tools.DoubleClicEvent;
import tools.DoubleClicListener;

public class DocumentChooser extends JDialog {

	private static final long serialVersionUID = -4275569487757998831L;

    private JLabel lblTitre;
    private JLabel lblIdentifiant;
    private JLabel lblNom;
    private JLabel lblDescription;
    
    private JTextField tfIdentifiant;
    private JTextField tfNom;
    private JTextField tfDescription;
    
    private JButton btnAnnuler;
    private JButton btnChoisir;
    
    private JScrollPane jScrollPane1;    
    private JList lResultat;
    
    private String valeurDefaut;

	protected EventListenerList listenerList;
	
	public DocumentChooser(JFrame p) {
		super(p,"Choix d'un document",true);
		
		setSize(300,400);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		initialisationComposants();
	}

	private void initialisationComposants() {
        lblTitre = new JLabel("Choisissez un document");
        valeurDefaut = "";
        lblIdentifiant = new JLabel("Identifiant : ");
        tfIdentifiant = new JTextField();
        
        lblNom = new JLabel("Nom : ");
        tfNom = new JTextField();
        
        lblDescription = new JLabel("Description :");
        tfDescription = new JTextField();
        
        
        btnAnnuler = new JButton("Annuler");
        btnAnnuler.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
        btnChoisir = new JButton("Choisir");
    	btnChoisir.setEnabled(false);
        
        lResultat = new JList();
        lResultat.setModel(new DefaultListModel());
        
        DefaultListSelectionModel m = new DefaultListSelectionModel();
        m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lResultat.setSelectionModel(m);
        lResultat.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
            	btnChoisir.setEnabled(true);
            }
          });
        
        jScrollPane1 = new JScrollPane();
        jScrollPane1.setViewportView(lResultat);
        
        placerComposants();
	}
	
	public void setValeurDefaut(String v){
		valeurDefaut = v;
	}
	
	public void addChoisirListener(ActionListener al){
		btnChoisir.addActionListener(al);
	}

	private void fireMyEvent(DoubleClicEvent evt) {
        Object[] listeners = listenerList.getListenerList();
        for (int i=0; i<listeners.length; i+=2) {
            if (listeners[i]==DoubleClicListener.class) {
                ((DoubleClicListener)listeners[i+1]).onDoubleClic(evt);
            }
        }
    }

	public String getValeurNom(){
		return tfNom.getText();
	}

	public String getValeurDescription(){
		return tfDescription.getText();
	}

	public String getValeurIdentifiant(){
		return tfIdentifiant.getText();
	}
	
	public String getValeurDocument(){
		return ((ComboItem)lResultat.getSelectedValue()).getValue();
	}
	
	private void placerComposants() {
		GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                            .addComponent(lblTitre, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(lblIdentifiant, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfIdentifiant, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblNom, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tfNom, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE))
                        .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(lblDescription, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tfDescription, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE))
                        .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(btnChoisir)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnAnnuler))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitre, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(lblIdentifiant)
                    .addComponent(tfIdentifiant, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNom)
                    .addComponent(tfNom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(tfDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDescription))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAnnuler)
                    .addComponent(btnChoisir))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
	}
	
	public void setResultat(Object[] objects){
		DefaultListModel mod = (DefaultListModel)lResultat.getModel();
		mod.clear();
		for (Object comboItem : objects) {
			mod.addElement(comboItem);
			if(((ComboItem) comboItem).getValue().equals(valeurDefaut)){
				lResultat.setSelectedValue(comboItem, true);
			}
		}
		
	}
	
	public void addChangeChampsRechercheListener(DocumentListener dl){
		tfNom.getDocument().addDocumentListener(dl);
		tfIdentifiant.getDocument().addDocumentListener(dl);
		tfDescription.getDocument().addDocumentListener(dl);
	}
	
}
